package de.zinulla.moviethek;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import de.zinulla.common.MoviethekConstants;
import de.zinulla.http.HttpHandler;
import de.zinulla.moviethek.db.MovieDataSource;

public class MoviethekActivity extends SherlockFragmentActivity {
	public static Context mycontext;
	private MovieDataSource datasource;
	private static final int DIALOG_SEARCH = 10;
	private static final int DIALOG_INFO = 20;
	private static final int REQUEST_CODE = 30;
	AlertDialog alertDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mycontext = this;

	} 

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SEARCH:

			LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
			final View layout = inflater.inflate(R.layout.search_dialog, null);
			Builder builder = new AlertDialog.Builder(this);
			builder.setCancelable(true);
			builder.setView(layout);
			// final AlertDialog alertDialog = builder.create();
			alertDialog = builder.create();
			alertDialog.setCancelable(true);

			// Cancel Button
			Button cancelButton = (Button) layout.findViewById(R.id.cancel);
			cancelButton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					alertDialog.cancel();
				}
			});
			// Search Button
			Button searchButton = (Button) layout.findViewById(R.id.search);
			searchButton.setOnClickListener(new View.OnClickListener() {
				private static final int REQUEST_CODE = 10;

				@Override
				public void onClick(View v) {

					EditText searchText = (EditText) layout.findViewById(R.id.searchTitle);
					Editable text = searchText.getText();
					String searchtext = text.toString();
					Intent i = new Intent(getApplicationContext(), SearchResultsActivity.class);
					i.putExtra(MoviethekConstants.SEARCH_TITLE, searchtext);
					startActivityForResult(i, REQUEST_CODE);
				}
			});

			alertDialog.show();
			return super.onCreateDialog(id);

		case DIALOG_INFO:
			// Info Dialog
			Dialog dialog = new Dialog(this);

			dialog.setContentView(R.layout.info_dialog);
			dialog.setTitle(R.string.info);

			dialog.show();
			return super.onCreateDialog(id);
		}
		return super.onCreateDialog(id);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// refreshList();
		String movietitel;
		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (scanResult != null) {
			String result = scanResult.getContents();
			movietitel = HttpHandler.getMovieTitelByEAN(result);
			Intent i = new Intent(this, SearchResultsActivity.class);
			i.putExtra(MoviethekConstants.SEARCH_TITLE, movietitel);
			startActivityForResult(i, REQUEST_CODE);
			// handle scan result
		}
		// else continue with any other code you need in the method
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		if (null != alertDialog) {
			alertDialog.dismiss();
		}
		super.onPause();
	}
}